package com.yllt4cloud.common.ddbase.param.jsonparam.tool;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * 简单包装fast-xml-jackson替换alibaba的fastjson
 *
 * @author LOpSSH
 */
public enum JSONMapper {
    INSTANCE;

    private final ObjectMapper mapper = new ObjectMapper();

    private final Logger logger = LoggerFactory.getLogger(JSONMapper.class);

    private JSONMapper() {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    public ObjectMapper getObjectMapper() {
        return mapper;
    }

    public <T> T readValue(String content, JavaType valueType) throws IOException {
        return mapper.readValue(content, valueType);
    }

    public <T> T readValue(String content, Class<T> valueType) throws IOException {
        return mapper.readValue(content, valueType);
    }

    public String writeValueAsString(Object object) throws JsonProcessingException {
        return mapper.writeValueAsString(object);
    }
}